﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wpf_Tool_ZC
{
    /// <summary>
    /// Wnd_Regedit.xaml 的交互逻辑
    /// </summary>
    public partial class Wnd_Regedit : Window
    {
        public Wnd_Regedit()
        {
            InitializeComponent();
        }

        private void Wnd_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow.Self.FwndRegedit = null;
        }

        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lPBuffer, int lpdwBufferLength);

        private const int INTERNET_OPTION_REFRESH = 0x000025;
        private const int INTERNET_OPTION_SETTINGS_CHANGED = 0x000027;

        /// <summary>
        /// 获取 代理服务器 当前状态
        /// </summary>
        void ProxyState_Get()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
            int iProxyEnable = (int)key.GetValue("ProxyEnable");
            key.Close();

            if (iProxyEnable == 0)
            {
                rbProxyDisable.IsChecked = true;
                MainWindow.WndLog_AppendText("Internet选项--连接--局域网设置--代理服务器 : 当前状态: 禁用");
            }
            else
            {
                rbProxyEnable.IsChecked = true;
                MainWindow.WndLog_AppendText("Internet选项--连接--局域网设置--代理服务器 : 当前状态: 启用");
            }
        }

        /// <summary>
        /// 设置 代理服务器 当前状态(启用/禁用)
        /// </summary>
        void ProxyState_Set(int _i)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
            key.SetValue("ProxyEnable", _i);
            key.Close();

            Reflush_RegInternetOption();
            if (_i == 0)
                MainWindow.WndLog_AppendText("代理服务器 状态 设置为 \"禁用\"");
            else
                MainWindow.WndLog_AppendText("代理服务器 状态 设置为 \"启用\"");
        }

        /// <summary>
        /// 刷新 Internet Option 的设置（改变）
        /// </summary>
        void Reflush_RegInternetOption()
        {
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
        }

        /// <summary>
        /// 获取 代理服务器 当前状态
        /// </summary>
        private void Click_ProxyState(object sender, RoutedEventArgs e)
        {
            ProxyState_Get();
        }

        /// <summary>
        /// 设置 代理服务器 当前状态 --> 启用
        /// </summary>
        private void Click_ProxyEnable(object sender, RoutedEventArgs e)
        {
            ProxyState_Set(1);
        }

        /// <summary>
        /// 设置 代理服务器 当前状态 --> 禁用
        /// </summary>
        private void Click_ProxyDisable(object sender, RoutedEventArgs e)
        {
            ProxyState_Set(0);
        }

        // ***

        const int WM_SETTINGCHANGE = 0x001A;
        const int HWND_BROADCAST = 0xffff;

        public enum SendMessageTimeoutFlags : uint
        {
            SMTO_NORMAL = 0x0000,
            SMTO_BLOCK = 0x0001,
            SMTO_ABORTIFHUNG = 0x0002,
            SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
        }

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessageTimeout(
            IntPtr windowHandle,
            uint Msg,
            IntPtr wParam,
            IntPtr lParam,
            SendMessageTimeoutFlags flags,
            uint timeout,
            out IntPtr result);

        /// <summary>
        /// 通知所有打开的程序注册表已修改 // ZC: 暂时 未使用该种方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Click_NotifyRegChange01(object sender, RoutedEventArgs e)
        {
            IntPtr result1;
            SendMessageTimeout(new IntPtr(HWND_BROADCAST), WM_SETTINGCHANGE, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out result1);
        }
    }
}
